
layui.config({
	version:'1510164124181'
}).use(['form','laydate', 'laypage', 'layer', 'element'],function(){
	form = layui.form;
	layer = layui.layer;
	$ = layui.jquery;
    laydate = layui.laydate;//日期
    laypage = layui.laypage;//分页
    element = layui.element; //元素操作
    pageNo=GetQueryString("pageNo");
    if(!isNotNull(pageNo)){
      pageNo=1;
    }
	TypelistByTypeId(2);//查询所有商品类型
	myrefresh();

});
function ProductListExport() {
    var identify ="";
    var type=$("#types option:selected").val();
	var title=$("#inputname").val();
    window.location.href=httpurl+"/ProductListExport?&title="+title+"&identify="+identify+"&type="+type;
}
function myrefresh(){
	var typeid=$("#types option:selected").val();
	var inputname=$("#inputname").val();
	$.ajax({
		url: httpurl+"/Productlistpc",
		headers: {
			"Access-Control-Allow-Origin": "*",
			"Access-Control-Allow-Methods": "*",
			"Access-Control-Allow-Headers": "Authorization",
			"Authorization": GetTokenInfo()
		},
		contentType : 'application/json',
		cache: false,
		data: {
			"pageNo":pageNo,
			"pageSize":pageRecords,
			"typeid":typeid,
			"inputname":inputname,
		},
		success: function(data){
			top.layer.closeAll();
			totalRecords =data.data.total;
			if(data.rtnCode==200){
				let dataList=data.data.list;
				HandleData(dataList);
			}else{
				let data_list = '<tr>'
					+'<td colspan="12" style="text-align:center;">暂无数据</td>'
					+'</tr>';
				$("#tableInfo tbody").html(data_list);

			}
			page();
		}
	});
}

function HandleData(initdata){
	let dataHtml="";
	for(let i=0;i<initdata.length;i++){
		dataHtml+='<tr>'+
						'<td><input type="checkbox" data-id="'+initdata[i].productEntity.id+'" name="box" class="aceChild"/></td>' +
						'<th>'+initdata[i].productEntity.identify+'</th>'+
						'<th>'+initdata[i].productEntity.title+'</th>'+
						'<td>'+(initdata[i].productEntity.typeEntity==null?"":initdata[i].productEntity.typeEntity.name)+'</td>'+
						'<td class="width20"><img id="preview" class="preview" src="'+visitimgurl+'/'+(initdata[i].productEntity==null?"":initdata[i].productEntity.headimg)+'" height="100" /></td>'+
						'<td>'+initdata[i].productEntity.intime+'</td>'+
						'<td>'+(initdata[i].productEntity.isshelves=="0"?"否":"是")+'</td>'+
						'<td colspan="2">' +
							'<table class="width100"><tbody>';
							let dataInfo=initdata[i].setMealEntity;
							for(let a=0;a<dataInfo.length;a++){
								dataHtml+='<tr><td class="width50">'+dataInfo[a].meal+'</td><td class="50%">'+dataInfo[a].stock+'</td></tr>';
							}
							dataHtml+='</tbody></table>' +
							'</td>' +
							'<td>' +
							'<a href="#" onclick="add_product('+initdata[i].productEntity.id+')">查看</a>&nbsp;&nbsp;' +
						'</td>'+
					'</tr>';
	}
	$("#tableInfo tbody").html(dataHtml);
}

//编辑商品
function add_product(pid){
	window.location.href="add_productQuery.html?productid="+pid+"&pageNo="+pageNo+"&tempname=ProductListQuery";
}


//分页
function page(){
	laypage.render({
		 elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
		,count:totalRecords //数据总数，从服务端得到
		,curr: pageNo //获取起始页 location.hash.replace('#!fenye=', '')
        ,hash: 'fenye' //自定义hash值
		,layout: ['count', 'prev', 'page', 'next', 'skip']
		,limit:pageRecords
		,jump: function(obj, first){
			//首次不执行
			if(!first){
		//	  console.log("当前页==="+obj.curr); //得到当前页，以便向服务端请求对应页的数据。
		//	  console.log("每页显示条数==="+obj.limit); //得到每页显示的条数
				pageNo=obj.curr;
				pageRecords=obj.limit;
				myrefresh();
			}
		  }
	});
}

function update_product_state(product_id,isshelves){
	top.layer.confirm('确认提交？',{icon: 3,title:'提示信息'},function(index){
		var index = top.layer.msg('提交中，请稍候',{icon: 16,time:false,shade:0.8});
		$.ajax({
			type:"POST",
			url: httpurl+"/UpdateProductState",
			cache: false,
			data: JSON.stringify({
				"id":product_id,
				"state":isshelves,
			}),
			dataType: "json",
			headers: {
				"Access-Control-Allow-Origin": "*",
				"Access-Control-Allow-Methods": "*",
				"Access-Control-Allow-Headers": "Authorization",
				"Authorization": GetTokenInfo()
			},
			contentType : 'application/json',
			success: function(data) {
				layer.closeAll();
				top.layer.msg(""+data.msg+"");
				if(data.rtnCode==200){
					myrefresh();
				}

			}
		});
	},function(){

	});
}
function search(){
	pageNo=1;
	isjump=0;
	myrefresh();
}
